import LowAppMenuType from '../constant/LowAppMenuType'

/**
 * 递归菜单
 * @param menuList
 * @param callback
 */
export function recursionMenuList(menuList, callback) {
  let isBreak = false
  let doBreak = () => isBreak = true
  let recursion = (list, parent) => {
    if (Array.isArray(list) && list.length > 0) {
      for (let menu of list) {
        callback(menu, parent, doBreak)
        if (isBreak) break
        if (menu.type === LowAppMenuType.GROUP.type) {
          recursion(menu.children, menu)
        }
        if (isBreak) break
      }
    }
  }
  recursion(menuList)
}

/***
 *  获取菜单的路由地址
 *
 *  o2ulDNOXGwvBRndehxW14dML+i/qxuVry2u3TXIhR9CrYEbhZi7RywQnYojV/SgC/PffdTE3WKU0/u0CXtgFzw==
 * @param menu
 * @return {string}
 */
export function getMenuRouterLink(menu) {
  if (menu.type === LowAppMenuType.CGFORM.type) {
    let {cgformThemeTemplate, cgformIsTree} = menu
    let pageType = 'normal'
    if (cgformThemeTemplate === 'erp') {
      pageType = 'erp'
    } else if (cgformThemeTemplate === 'innerTable') {
      pageType = 'inner'
    } else if (cgformThemeTemplate === 'tab') {
      pageType = 'tab'
    } else if (cgformIsTree === 'Y') {
      pageType = 'tree'
    }
    return `/app/${menu.appId}/${menu.type}/${pageType}/${menu.menuUrl}`
  } else {
    return `/app/${menu.appId}/${menu.type}/${menu.menuUrl}`
  }
}

/**
 * 判断是否是“开发模式”下才允许访问的菜单
 * @param name
 */
export function isDevModeMenu(name) {
  // 如果不是开发模式，那就不允许跳转到以下几个页面
  switch (name) {
    // 应用详情首页
    case 'low-app-@appId':
    // 应用功能页面
    case 'low-app-@appId-list-cgform':
    case 'low-app-@appId-list-desform':
    case 'low-app-@appId-list-cgreport':
    case 'low-app-@appId-list-graphreport':
    // 回收站
    case 'low-app-@appId-menu-recycle-bin':
      return true
    default:
      return false
  }
}
